package com.csu.cphone.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.RecyclePhoneMapper;
import com.csu.cphone.pojo.RecyclePhonePojo;
import com.csu.cphone.pojo.RecycleProblemPojo;
import com.csu.cphone.service.RecyclePhoneService;
import com.csu.cphone.service.RecycleProblemService;

/**
 * @项目名称：cphone
 * @类名称：RecyclePhoneServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月7日 下午9:45:52
 * @company:
 */
@Service
public class RecyclePhoneServiceImpl implements RecyclePhoneService
{
    @Autowired
    private RecyclePhoneMapper recyclePhoneMapper;

    @Autowired
    private RecycleProblemService recycleProblemService;

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#add(java.lang.Integer,
     *      java.lang.String, java.lang.String, java.lang.Double,
     *      java.lang.Integer)
     */
    @Override
    public RecyclePhonePojo add(Integer brandId, String name, String picUrl, Double totalPrice, Integer isHot,
            List<RecycleProblemPojo> problems)
    {
        RecyclePhonePojo recyclePhone = new RecyclePhonePojo();
        recyclePhone.setBrandId(brandId);
        recyclePhone.setName(name);
        recyclePhone.setPicUrl(picUrl);
        recyclePhone.setStatus(0);
        recyclePhone.setTotalPrice(totalPrice);
        recyclePhone.setIsHot(isHot);
        recyclePhoneMapper.insert(recyclePhone);
        if (!CollectionUtils.isEmpty(problems))
        {
            for (RecycleProblemPojo problem : problems)
            {
                recycleProblemService.add(recyclePhone.getId(), problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
            }
        }

        return recyclePhone;
    }

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#update(com.csu.cphone.pojo.RecyclePhonePojo)
     */
    @Override
    public Integer update(RecyclePhonePojo recyclePhone)
    {
        return recyclePhoneMapper.update(recyclePhone);
    }

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#queryById(java.lang.Integer)
     */
    @Override
    public RecyclePhonePojo queryById(Integer recyclePhoneId)
    {
        return recyclePhoneMapper.selectById(recyclePhoneId);
    }

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#count()
     */
    @Override
    public Integer count()
    {
        return recyclePhoneMapper.count();
    }

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#queryList(java.lang.Integer,
     *      java.lang.Integer, java.lang.String)
     */
    @Override
    public List<RecyclePhonePojo> queryList(Integer pageSize, Integer startIndex, String order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);

        return recyclePhoneMapper.queryList(map);
    }

    /**
     * @see com.csu.cphone.service.RecyclePhoneService#deleteById(java.lang.Integer)
     */
    @Override
    public Integer deleteById(Integer recyclePhoneId)
    {
        return recyclePhoneMapper.deleteById(recyclePhoneId);
    }

}
